package com.learm.classLoad;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MyClassLoader extends ClassLoader {
    private String byteCode_Path;

    public MyClassLoader( String byteCode_Path){
        this.byteCode_Path = byteCode_Path;
    }

    @Override
    protected  Class<?> findClass(String className){

        byte value[] = null;
        BufferedInputStream in = null;

        try {
            in = new BufferedInputStream(new FileInputStream(byteCode_Path+className+".class"));
            value = new byte[in.available()];
            in.read(value);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null != in){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return defineClass(value, 0, value.length);
    }

    public static void main(String[] args) throws ClassNotFoundException {
        MyClassLoader classLoader = new MyClassLoader("F:/");
        System.out.println("加载目标类的类加载器->" + classLoader.loadClass("TestAction").getClassLoader().getClass().getName());

        System.out.println("当前类加载器的超类加载器->" + classLoader.getParent().getClass().getName());
    }

}
